package com.hw.hcit.feature;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

public class FMTest extends JTestItem implements OnClickListener {
	private static final String LOG_TAG = "FMTest";
	final static String FREQ_CHANGE = "com.thunderst.radio.fmchange";
	private Context mContext;
	private boolean mHeadsetExist = false;
	private TextView mChannelName;
	private TextView mNoHeadset;
	private Button mNextChannelBtn;
	private double[] mChannelList = new double[] { 101.7, 103.7, 107.7 };
	// private double[] mChannelList = new double[] { 88, 98, 107 };
	private int mButtonClickCount = 0;
	private boolean mPowerClicked;
	private Button mButton1,mButton2;

	public FMTest(JCtrlCallback cb) {
		super(cb);
	}

	public FMTest() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fm_test, container, false);
		mChannelName = (TextView) rootView.findViewById(R.id.channel_name);
		mNoHeadset = (TextView) rootView.findViewById(R.id.no_headset_warning);
		mButton1 = (Button) rootView.findViewById(R.id.button1);
		mButton2 = (Button) rootView.findViewById(R.id.button2);
		mButton1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(19, 'P');
					} else {
						rewriteOneByteOfNv2499(50, 'P');
					}
				}
				 testFinish(true, 1, true);
				
			}
		});
		mButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(19, 'F');
					} else {
						rewriteOneByteOfNv2499(50, 'F');
					}
				}
				testFinish(false, 1, false);
				
			}
		});
		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mContext = activity;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mButton1.setEnabled(false);
		mPowerClicked = false;
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_HEADSET_PLUG);
		filter.addAction(FREQ_CHANGE);
		mContext.registerReceiver(mReceiver, filter);
		if (mHeadsetExist) {
			mNoHeadset.setVisibility(View.GONE);
			Intent send = new Intent("com.thunderst.radio.service.trunon");
			send.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
			send.putExtra("freq", 103.7);
//			mChannelName.setText(getNextChannel() + "MHz");
			mContext.sendBroadcast(send);
		} else {
			mNoHeadset.setVisibility(View.VISIBLE);
		}

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Intent i = new Intent(
				"com.thunderst.radio.service.trunoff.and.ondestroyservice");
		i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
		mContext.sendBroadcast(i);
		mContext.unregisterReceiver(mReceiver);
		mButtonClickCount = 0;
	}

	@Override
	public boolean getResult() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getResultInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startTest() {
		// TODO Auto-generated method stub

	}

	@Override
	public void stopTest() {
		// TODO Auto-generated method stub

	}

	public void refresh() {

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}

	BroadcastReceiver mReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
				Log.d(LOG_TAG, LOG_TAG + "ACTION_HEADSET_PLUG");
				if (intent.getIntExtra("state", 0) == 0) {
					if (mHeadsetExist) {
						mContext.sendBroadcast(new Intent(
								"com.thunderst.radio.service.trunoff.and.ondestroyservice"));
					}
					mHeadsetExist = false;
					mNoHeadset.setVisibility(View.VISIBLE);
				} else {
					mHeadsetExist = true;
					mNoHeadset.setVisibility(View.GONE);
					Intent send = new Intent(
							"com.thunderst.radio.service.trunon");
					send.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
					send.putExtra("freq", 103.7);
//					mChannelName.setText(getNextChannel() + "MHz");
					mContext.sendBroadcast(send);
				}
			} else if (FREQ_CHANGE.equals(action)) {
				String now = getResources().getString(R.string.fm_freqnow);
        		String kmz = getResources().getString(R.string.fm_khz);
        		float fm = intent.getFloatExtra("fm",0);   
        		Log.d("FMService", "receive--->"+fm);
        		if (fm != 0){
//        			float f =(float)fm;
        			mChannelName.setText(now+""+fm+kmz);	
				}
        		mButton1.setEnabled(true);
			}
		}
	};

/*	private double getNextChannel() {
		double channel = mChannelList[mButtonClickCount % 3];
		Log.d(LOG_TAG, LOG_TAG + "mButtonClickCount = " + mButtonClickCount);
		Log.d(LOG_TAG, LOG_TAG + "getNextChannel = " + channel);
		return channel;
	}*/

/*	View.OnClickListener mOnClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.next_channel:
				mButtonClickCount++;
				mNextChannelBtn.setEnabled(false);
				Intent i = new Intent(
						"com.thunderst.radio.service.trunoff.and.ondestroyservice");
				i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
				mContext.sendBroadcast(i);
				Intent send = new Intent("com.thunderst.radio.service.trunon");
				send.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
				send.putExtra("freq", getNextChannel());
				mChannelName.setText(getNextChannel() + "MHz");
				mContext.sendBroadcast(send);
				break;
			default:
				break;
			}
		}
	};*/

	 @Override
	    public boolean dispatchKeyEvent(KeyEvent event) {
	        // TODO Auto-generated method stub
	        return true;
	    }
	/*@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
			Log.d(LOG_TAG, LOG_TAG + "dispatchKeyEvent, mButtonClickCount = "
					+ mButtonClickCount);
			Log.d(LOG_TAG, LOG_TAG + "mNextChannelBtn.isEnabled() = "
					+ mNextChannelBtn.isEnabled());
			if (mPowerClicked) {
				testFinish(false, 1, false);
				return true;
			}
			if (!((mButtonClickCount % 3) == 2) || !mNextChannelBtn.isEnabled()) {
				showTestUncomplete();
				return true;
			}
		} else if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
			mPowerClicked = true;
			showPowerClicked();
			return true;
		}
		return super.dispatchKeyEvent(event);
	}*/

}
